<template>
  <div id="tasks">
    <div class="card">
      <div class="card-header bg-info text-white">任务列表</div>
      <div class="card-body">
        <form class="row row-cols-lg-auto align-items-center">
          <label>类型</label>
          <div class="col-1">
            <select class="form-select" v-model.number="taskType">
              <option value="-1">所有</option>
              <option value="0">一般通知</option>
              <option value="1">选择通知</option>
              <option value="2">每日审核</option>
            </select>
          </div>
          <label>每页记录数</label>
          <div class="col-1">
            <select class="form-select" v-model.number="sc.pageSize">
              <option value="5">5</option>
              <option value="10">10</option>
              <option value="20">20</option>
              <option value="50">50</option>
            </select>
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-primary"
              @click="search()"
            >
              查找
            </button>
            <button
              type="button"
              class="btn btn-secondary ms-2"
              title="清空查找条件"
              @click="reset()"
            >
              重置
            </button>
          </div>
        </form>
      </div>
      <table
        class="table table-response table-striped table-hover table-sm"
        id="tableList"
      >
        <thead>
          <tr>
            <th>ID</th>
            <th class="">标题</th>
            <th class="">内容</th>
            <th class="">任务类型</th>
            <th class="">订单类型</th>
            <th class="">订单ID</th>
            <th>日期</th>
            <th>状态</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(info, index) in dataList">
            <td>
              {{ info.id }}
            </td>
            <td class="">
              {{ info.title }}
            </td>
            <td class="">
              <template
                v-if="info.content != null && info.content.length > 100"
              >
                {{ info.content.substring(0, 100) }}
              </template>
              <template v-else>
                {{ info.content }}
              </template>
            </td>
            <td class="">
              <span v-if="info.taskType === 0">通知</span>
              <span v-else-if="info.taskType === 1">选择通知</span>
              <span v-else-if="info.taskType === 2">每日审核</span>
              <span v-else>{{ info.taskType }}</span>
            </td>
            <td>
              <span v-if="info.orderType === 1040">机票订单取消</span>
              <span v-else-if="info.orderType === 1">选择通知</span>
              <span v-else-if="info.orderType === 2">每日审核</span>
              <span v-else>{{ info.orderType }}</span>
            </td>
            <td>
              <span v-if="info.orderId > 0">{{ info.orderId }}</span>
            </td>
            <td>{{ info.createDate }}</td>
            <td>
              <template v-if="info.taskType === 2">
                <button
                  class="btn btn-success"
                  @click.stop="finishTask(info.id)"
                  v-if="info.status === 0"
                >
                  完成
                </button>
                <span v-if="info.status === 1">已完成</span>
                <button
                  class="btn btn-secondary btn-sm ms-2"
                  @click.stop="resend(info.id)"
                  v-if="info.status === 1"
                >
                  再次发送
                </button>
              </template>
            </td>
            <td>
              {{ info.createTime }}
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>
  </div>
</template>

<script>
import {
  searchTasks,
  finishDailyAuditTask,
  resendDailyAuditTask,
} from "@/api/misc.js";
import MyPagination from "@/components/my-pagination.vue";

export default {
  components: {
    MyPagination,
  },
  data() {
    return {
      taskType: 2,

      dataList: [],
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
    };
  },
  mounted: function () {
    this.search();
  },
  activated: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      const params = {
        "sc.type": this.taskType,
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
      };

      searchTasks(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
      });
    },
    reset: function () {
      this.taskType = 2;
    },
    finishTask: function (taskId) {
      finishDailyAuditTask(taskId, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("操作成功");
          this.search();
        } else {
          this.showErrMsg("操作失败", "danger");
        }
      });
    },
    resend: function (taskId) {
      resendDailyAuditTask(taskId, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("操作成功");
          this.search();
        } else {
          this.showErrMsg("操作失败: " + v.errmsg, "danger");
        }
      });
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
